//
//  XMN_SetingViewController.m
//  B段项目
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 谢美娜. All rights reserved.
//

#import "XMN_SetingViewController.h"
#import "XMN_Seting_1.h"
#import "XMN_Seting_2.h"
#import "XMN_OpinionViewController.h"
#import "XMN_AboutViewController.h"

@interface XMN_SetingViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *dataArray;
@property(nonatomic, strong)NSArray *imageArray;
@property(nonatomic, assign)NSInteger nightFlag;
@property(nonatomic, assign)NSInteger fontFlag;

@property(nonatomic, strong)UIView *backView;
@property(nonatomic, strong)myView *fontView;
@property(nonatomic, strong)UITableView *fontTableView;
@property(nonatomic, strong)NSArray *fontArray;

//清理缓存
@property(nonatomic, strong)CleanCacheFile *clean;
@property(nonatomic, strong)NSArray *array;
@end

static NSInteger fontIndex;

@implementation XMN_SetingViewController

-(void)loadView{
    [super loadView];
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        fontIndex = 1;
    });
    _dataArray = [NSArray arrayWithObjects:@"字体大小", @"夜间模式", @"意见反馈", @"清除缓存", @"关于", nil];
    _imageArray = [NSArray arrayWithObjects:@"XMN_font", @"XMN_son", @"XMN_opinion", @"XMN_clean", @"XMN_about", nil];
    _fontArray = [NSArray arrayWithObjects:@"大", @"中", @"小", nil];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.bounces = NO;
    _tableView.tableFooterView = [[UIView alloc]init];
    [_tableView NightWithType:UIViewColorTypeNormal];
    [self.view addSubview:_tableView];
    
    //寻找沙盒路径
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).lastObject;
    NSString *cachePath = [path stringByAppendingPathComponent:@"Caches"];
    _array = [NSArray arrayWithObjects:cachePath, nil];
    _clean = [[CleanCacheFile alloc]init];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"设置";
    [self createFontView];
    // Do any additional setup after loading the view.
}

#pragma mark - tableView的相关设置
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _fontTableView) {
        return _fontArray.count;
    }
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _fontTableView) {
        static NSString *identifier = @"fontCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        switch (indexPath.row) {
            case 0:{
                cell.textLabel.font = [UIFont systemFontOfSize:BIGContentSize];
                break;
            }
            case 1:{
                cell.textLabel.font = [UIFont systemFontOfSize:MIDDLEContentSize];
                break;
            }
            case 2:{
                cell.textLabel.font = [UIFont systemFontOfSize:SMALLContentSize];
                break;
            }
            default:
                break;
        }
        cell.textLabel.text = [_fontArray objectAtIndex:indexPath.row];
        if (indexPath.row == fontIndex) {
            cell.textLabel.textColor = [UIColor colorWithRed:0.54 green:0.78 blue:0.64 alpha:1];
        }else{
            cell.textLabel.textColor = [UIColor grayColor];
        }
        [cell NightWithType:UIViewColorTypeNormal];
        //tableView点击样式
        UIView *cellBackView = [[UIView alloc]initWithFrame:cell.frame];
        cellBackView.backgroundColor = [UIColor whiteColor];
        cell.selectedBackgroundView = cellBackView;
        return cell;
    }
    if (indexPath.row % 2 == 1) {
        static NSString *identifier = @"cell";
        XMN_Seting_2 *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[XMN_Seting_2 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.leftImageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
        cell.rightLabel.text = [_dataArray objectAtIndex:indexPath.row];
        if (indexPath.row == 1) {
            if ([[NSUserDefaults standardUserDefaults] boolForKey:@"nightModel"]) {
                cell.leftImageView.image = [UIImage imageNamed:@"XMN_moon"];
            }else{
                cell.leftImageView.image = [UIImage imageNamed:@"XMN_son"];
            }
        }
        if (indexPath.row == 3) {
            cell.rightLabel.text = [NSString stringWithFormat:@"%@(%.2fM)", [_dataArray objectAtIndex:indexPath.row], [_clean folderSizeAtCachePath:_array]];
        }
        
        //tableView点击样式
        UIView *cellBackView = [[UIView alloc]initWithFrame:cell.frame];
        cellBackView.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:.2];
        cell.selectedBackgroundView = cellBackView;
        
        return cell;
    }
    static NSString *identifier = @"cell";
    XMN_Seting_1 *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[XMN_Seting_1 alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.leftImageView.image = [UIImage imageNamed:[_imageArray objectAtIndex:indexPath.row]];
    cell.rightLabel.text = [_dataArray objectAtIndex:indexPath.row];
//    [cell NightWithType:UIViewColorType1];
    //tableView点击样式
    UIView *cellBackView = [[UIView alloc]initWithFrame:cell.frame];
    cellBackView.backgroundColor = [UIColor colorWithRed:.9 green:.9 blue:.9 alpha:.2];
    cell.selectedBackgroundView = cellBackView;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _fontTableView) {
        return 40 * FitHeight;
    }else{
        if (indexPath.row % 2 == 1) {
            return 70 * FitHeight;
        }
        return 50 * FitHeight;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == _fontTableView) {
        fontIndex = indexPath.row;
        _backView.userInteractionEnabled = NO;
        [UIView animateWithDuration:.5 animations:^{
            _backView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0];
            _fontView.frame = CGRectMake(ScreenWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight);
        }];
        _fontFlag = 0;
        switch (indexPath.row) {
            case 0:{
                [[FontSizeManage ChangeSize] setFontSize:0];
                break;
            }
                
            case 1:{
                [[FontSizeManage ChangeSize] setFontSize:1];
                break;
            }
            case 2:{
                [[FontSizeManage ChangeSize] setFontSize:2];
                break;
            }
                
            default:
                break;
        }
        [_fontTableView reloadData];
    }else{
        
        XMN_Seting_2 *cell = [tableView cellForRowAtIndexPath:indexPath];
        switch (indexPath.row) {
            case 0:{
                _fontView.frame = CGRectMake(-ScreenWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight);
                if (_fontFlag == 0) {
                    _backView.userInteractionEnabled = YES;
                    [UIView animateWithDuration:.5 animations:^{
                        _backView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:.7];
                        _fontView.frame = CGRectMake(50 * FitWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight);
                    }];
                    _fontFlag = 1;
                }else{
                    _backView.userInteractionEnabled = NO;
                    [UIView animateWithDuration:.5 animations:^{
                        _backView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0];
                        _fontView.frame = CGRectMake(ScreenWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight);
                    }];
                    _fontFlag = 0;
                }
                break;
            }
            case 1:{
                if ([[NSUserDefaults standardUserDefaults] boolForKey:@"nightModel"]) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"WebNight" object:@"0"];
                    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"nightModel"];
                    [ThemeManage shareThemeManage].isNight = 0;
                    
                    cell.leftImageView.image = [UIImage imageNamed:@"XMN_son"];
                }else{
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"WebNight" object:@"1"];
                    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"nightModel"];
                    [ThemeManage shareThemeManage].isNight = 1;
                    cell.leftImageView.image = [UIImage imageNamed:@"XMN_moon"];
                }
                [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:nil];
                break;
            }
            case 2:{
                XMN_OpinionViewController *opinionVC = [[XMN_OpinionViewController alloc]init];
                [self.navigationController pushViewController:opinionVC animated:YES];
                break;
            }
            case 3:{
                DXAlertView *cleanAlertView = [[DXAlertView alloc]initWithTitle:@"是否清除缓存?" contentText:[NSString stringWithFormat:@"%.2fM", [_clean folderSizeAtCachePath:_array]] leftButtonTitle:@"取消" rightButtonTitle:@"确定"];
                [cleanAlertView show];
                cleanAlertView.rightBlock = ^(){
                    [_clean cleanCacheFileAtCachePath:_array];
                    [_tableView reloadData];
                };
                break;
            }
            case 4:{
                XMN_AboutViewController *aboutVC = [[XMN_AboutViewController alloc]init];
                [self.navigationController pushViewController:aboutVC animated:YES];
                break;
            }
                
            default:
                break;
        }
    }
}

#pragma mark - 创建字体大小的视图
-(void)createFontView{
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, Height)];
    _backView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0];
    _backView.userInteractionEnabled = NO;
    [self.view addSubview:_backView];
    
    _fontView = [[myView alloc]initWithFrame:CGRectMake(-ScreenWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight)];
    [self.view addSubview:_fontView];
    
    UILabel *titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(30 * FitWidth, 20 * FitHeight, _fontView.frame.size.width - 60 * FitWidth, 50 * FitHeight)];
    titleLabel.text = @"字体大小";
    titleLabel.font = [UIFont systemFontOfSize:17];
    titleLabel.textColor = [UIColor grayColor];
    [_fontView addSubview:titleLabel];
    
    _fontTableView = [[UITableView alloc]initWithFrame:CGRectMake(20 * FitWidth, 80 * FitHeight, _fontView.frame.size.width - 40 * FitWidth, 120 * FitHeight)];
    _fontTableView.delegate = self;
    _fontTableView.dataSource  = self;
    [_fontTableView NightWithType:UIViewColorTypeNormal];
    [_fontView addSubview:_fontTableView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(titleLabel.frame.origin.x, _fontTableView.frame.origin.y + _fontTableView.frame.size.height + 20 * FitHeight, titleLabel.frame.size.width, 30 * FitHeight);
    [button setTitle:@"取消" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    [_fontView addSubview:button];
    
}

-(void)cancelAction{
    _backView.userInteractionEnabled = NO;
    [UIView animateWithDuration:.5 animations:^{
        _fontView.frame = CGRectMake(ScreenWidth, 120 * FitHeight, ScreenWidth - 100 * FitWidth, 270 * FitHeight);
        _backView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0];
    }];
    _fontFlag = 0;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
